<?php
class admin extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = "管理员";
		$this->modelObj = D('company_admin');
		$this->roleObj = D('company_role');

		$this->cityObj = D('sys_city');
		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');

		$this->userObj = D('sys_user');

		$this->data_list = $this->roleObj->get_treeList("status=1".$this->cond_base);

	}

	//列表
	public function index(){
		$this->tabtit = array('全部','禁用');

		$cond = "is_admin=0 and level=0 ".$this->cond_base;
		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and status=1";
		}
		//搜索
		if($this->keyword){
			$cond.=" and (name like '%".$this->keyword."%') ";
		}

		$result = $this->modelObj->get_pagelist($cond);
        if($result){
			$this->roles = $this->roleObj->ids($result['list'],'role_id');
			$this->users = $this->userObj->ids($result['list'],'user_id');
			// dump($this->roles);exit;
        	$this->list = $result['list'];
			$this->showPage = $result['showPage'];
			$this->total = $result['total'];
		}

	}

	//添加
	public function add(){
		if($this->isAjax()){
			$data = $this->postData('data');
			$res = $this->modelObj->check_user($data['username']);
			if($res){
				$this->renderError('用户名已存在');
			}

			//微信绑定
			$cond = "tel='{$data['tel']}' and status=1";
			$user = $this->userObj->find(array($cond));
			if($user){
				$data['user_id'] = $user['id'];
			}

			$data['company_id'] = $this->company['id'];
			$this->modelObj->add_user($data);
			$this->renderSuccess('添加成功', "/{$this->routeUrl}/index");
		}
		$this->info = array('status'=>1,'sex'=>1,'level'=>$this->tab);

		// dump($this->accessList);exit;
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			$this->modelObj->edit_user($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}

		$info = $this->modelObj->findById($id);
		$info['user'] = $this->userObj->findById($info['user_id']);
		$this->info = $info;

		// $cond = "status=1".$this->cond_base;
		// $this->areaList = $this->cityObj->getJsTree($cond,$this->info['area_ids']);
		// $this->cateList = $this->cateObj->getJsTree($cond,$this->info['cate_ids']);
		// dump($this->cateList);exit;
		$this->display($this->routeUrl."/set.php");
	}

    //隐藏
	public function ban(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->act($id,0);
			$this->renderSuccess('隐藏成功');
		}
	}

	//显示
	public function act(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->act($id,1);
			$this->renderSuccess('显示成功');
		}
	}

    //删除
	public function del(){
		$id = arg('id');
		if($this->isAjax()){
			$res = $this->modelObj->del($id);
			$this->renderSuccess('删除成功');
		}
	}

	//排序
	public function sort() {
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
    }

	//管理
	public function login(){
		$id = arg('id');
		$cond = "id='{$id}' and status=1 ".$this->cond_base;
		$user = $this->modelObj->find(array($cond));

		$_SESSION['company_user']['id'] = $user['id'];
        $_SESSION['logintime'] = time();
        // set_cookie('company_admin_mnm',$user['username'],time()+3600*24);

		$this->redirect('/index/main/index');
	}

	// 修改密码
	public function pwd(){
		if($this->isAjax()){

			$data = $this->postData('data');
			$data['username'] = $this->admin['username'];
			if ($data['password'] !== $data['password_confirm']) {
				$this->renderError('确认密码不正确');
			}
	        $admin = $this->modelObj->pwd_user($data);
	        if($admin){
				// $_SESSION["admin"] = $admin;
	            $this->renderSuccess('更新成功', '/company/login/logout');
	        }else{
	            $this->renderError('更新失败');
	        }

		}
	}

	//编辑
	public function auth(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');

			$arrBase['area_ids'] = joinStr($data['area']);
			$arrBase['cate_ids'] = joinStr($data['cate']);
			$arrBase['zone_ids'] = joinStr($data['zone']);

			$this->modelObj->edit($id,$arrBase);
			$this->renderSuccess('授权成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);

		// if($this->site['city_ids']){
		// 	$cond = "status=1 and id IN ({$this->site['city_ids']})";
		// 	$this->areaList = $this->cityObj->get_JsTree($cond,$this->info['area_ids']);
		// }
		// if($this->site['cate_ids']){
		// 	$cond = "status=1 and id IN ({$this->site['cate_ids']})";
		// 	$this->cateList = $this->cateObj->get_JsTree($cond,$this->info['cate_ids']);
		// }
		$cond_area = "status=1 ".$this->cond_base;
		$cond_cate = "status=1 ".$this->cond_base;
		$cond_zone = "status=1 ".$this->cond_base;
		$this->areaList = $this->areaObj->get_JsTree($cond_area,$this->info['area_ids']);
		$this->cateList = $this->cateObj->get_JsTree($cond_cate,$this->info['cate_ids']);
		$this->zoneList = $this->zoneObj->get_JsTree($cond_zone,$this->info['zone_ids']);
	}

	//微信绑定
	public function wxbind(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('wxbind');
			$cond = "tel='{$data['tel']}' and status=1";
			$user = $this->userObj->find(array($cond));
			if($user){
				$arrBase['user_id'] = $user['id'];
				$this->modelObj->edit($id,$arrBase);
				$this->renderSuccess('绑定成功');
			}else{
	            $this->renderError('手机号不存在');
	        }

		}
	}
}